//
//  TMCoursOrderController.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/7/18.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMCoursOrderController.h"
#import "TMCourseSaleCell.h"
#import "TMUserViewModel.h"
#import "TMTradeDetailController.h"
#import "TMMyCourseModel.h"
@interface TMCoursOrderController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *orderCollection;
@property (nonatomic, assign) CGSize itemSize;
@property (nonatomic, strong) TMUserViewModel *viewModel;
@property (nonatomic, strong) NSArray *listArray;
@property (nonatomic, strong) WZNoResultView *noResultView;
@end

@implementation TMCoursOrderController
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (kWindowH == 812.0) {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        [self.orderCollection addSubview:_noResultView];
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
-(NSArray *)listArray{
    if (!_listArray) {
        _listArray = [NSArray array];
    }
    return _listArray;
}
- (void)setOrderCollection:(UICollectionView *)orderCollection{
    _orderCollection = orderCollection;
    _orderCollection.delegate = self;
    _orderCollection.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 12.;
    flowLayout.minimumInteritemSpacing = 0.;
    _itemSize = CGSizeMake(173 * kWindowW /375, 246 * kWindowW /375);
    _orderCollection.collectionViewLayout = flowLayout;
    [_orderCollection registerNib:[UINib nibWithNibName:@"TMCourseSaleCell" bundle:nil] forCellWithReuseIdentifier:@"TMCourseSaleCell"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程预定";
    self.viewModel = [[TMUserViewModel alloc]init];
    self.orderCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.viewModel getCourseOrderSuccessSuccess:^(id responseObject) {
            self.listArray = responseObject;
            [self noResultViewShowORNot];
            [self.orderCollection reloadData];
            [self.orderCollection.mj_header endRefreshing];
        } Failed:^(NSString *error) {
            [self noResultViewShowORNot];
            [self.orderCollection reloadData];
             [self.orderCollection.mj_header endRefreshing];
        }];
    }];
    [self.orderCollection.mj_header beginRefreshing];
}
#pragma delegate datas

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return self.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(7, 12, 0, 12);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TMCourseSaleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMCourseSaleCell" forIndexPath:indexPath];
    cell.isOrder = YES;
    cell.courseModel = self.listArray[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    TMMyCourseModel *models = self.listArray[indexPath.row];
    TMTradeDetailController *vc = [[UIStoryboard storyboardWithName:@"Mine" bundle:nil] instantiateViewControllerWithIdentifier:@"TMTradeDetailController"];
    vc.cid = models.cid;
    vc.order_id = models.bid;
    vc.isorder = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
